<?php
namespace common\behaviors;

use Yii;
use yii\base\Behavior;
use common\component\exception\ErrMap;
use common\models\OperationLog;
use common\models\Album;
use common\models\AlbumCategory;
use common\component\tool\Tool;


class Albums extends Behavior
{
    /*相册列表*/
    public function getAlbumCategoryList($com_id, $app_id = 0)
    {
        $albumCategoryList = AlbumCategory::find()
                    ->select(['cat_id', 'cat_name', 'cover_url', 'album_number', 'album_size', 'sort', 'add_time'])
                    ->where('app_id = :app_id and com_id = :com_id', [':app_id' => $app_id, ':com_id' => $com_id])
                    ->orderBy(['sort' => SORT_DESC])->asArray()->all();
        return Tool::showRes([0, '获取成功'], $albumCategoryList);
    }
    
    
    /*创建相册*/
    public function addAlbumCategoty($post, $com_id, $app_id = 0)
    {
        if(Yii::$app->request->isPost){
            $albumCategory = new AlbumCategory;
            if($cat_id = $albumCategory->addCategory($post, $com_id, $app_id)){
                $backInfo = [0, '添加相册列表成功'];//返回信息
            }else{
                if($albumCategory->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $albumCategory->getErrors();//异常信息
                    if(array_key_exists("repeatName", $exception)){
                        $backInfo = ErrMap::getError(10015);//返回信息
                    };
                }
            }
        };

        if(isset($cat_id) and $cat_id != false){
            $backData = ['cat_id' => $cat_id];//返回值
        }else{
            $backData = '';
        }
        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception, $backData) ) != 'boolean'){
            return $res;
        };
    }

    /*获取相册的图片*/
    public function albumList($post, $com_id)
    {
        if(isset($post['page'])){
            $currPage = intval($post['page']?$post['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = 20;//前台组件默认最多显示20张

        $where = 'com_id = ' . $com_id;
        if(isset($post['cat_id']) and !empty($post['cat_id'])){
            $where .= ' and cat_id = ' . $post['cat_id'];
        }
        // P($where);
        $album = new Album;
        $count = $album::find()->where($where)->count();
        $albumList = $album::find()->where($where)
                    ->select(['album_name', 'album_path', 'is_cover', 'diff_size', 'sort', 'add_time'])
                    ->offset($pageSize*($currPage-1))->limit($pageSize)
                    ->orderBy(['sort' => SORT_DESC])->asArray()->all();
        /*转成300*300尺寸返回*/
        foreach($albumList as $k => $v){
            $albumList[$k]['album_path'] = Tool::getAlbumBySizenum($v['album_path'], $v['diff_size']);
        }
        // P($albumList);

        $res = array(
            'list' => $albumList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*图片上传*/
    public function upload($post, $uploadFileName, $com_id, $app_id = 0)
    {
        // P($com_id);
        // P($_FILES);
        if(isset($post['catId'])){//有相册ID
            if($_FILES){//有图片上传
                $album = new Album();
                if($fileInfo = $album->upload($post, $uploadFileName, $com_id, $app_id)){
                    $backInfo = [0, '上传成功'];//返回信息
                }else{
                    if($album->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $album->getErrors();//异常信息
                    }
                }
            }
        }else{//没有相册ID  
            $backInfo = ErrMap::getError(10016);//返回信息
        }

        if(!isset($fileInfo)){
            $fileInfo = [];//返回数据
        }
        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception, $fileInfo) ) != 'boolean'){
            return $res;
        };
    }
    

    
}
